package com.zidongxiangxi.rabbit.consumer.sql;

import org.springframework.util.StringUtils;

/**
 * 消费记录sql提供者
 *
 * @author chenxudong
 * @date 2020/11/07
 */
public class ConsumeRecordSqlProvider {
	private static final String DEFAULT_TABLE_NAME = "consume_record";
	private static final String SELECT_ID_SQL = "select id from %s where create_time<? order by "
			+ "create_time limit ?";
	private static final String EXIST_SQL = "select 1 from %s where produce_application=? and consume_application=? and message_id=?";
	private static final String INSERT_SQL = "insert into %s (produce_application, consume_application, message_id) " +
			"values (?, ?)";
	private static final String DELETE_SQL = "delete from %s where produce_application=? and consume_application=? and message_id=?";
	private static final String BATCH_DELETE_SQL = "delete from %s where id in (:ids)";

	private final String selectIdSql, existSql, insertSql, deleteSql, batchDeleteSql;

	public ConsumeRecordSqlProvider(String tableName) {
		tableName = StringUtils.isEmpty(tableName) ? DEFAULT_TABLE_NAME : tableName;
		selectIdSql = String.format(SELECT_ID_SQL, tableName);
		existSql = String.format(EXIST_SQL, tableName);
		insertSql = String.format(INSERT_SQL, tableName);
		deleteSql = String.format(DELETE_SQL, tableName);
		batchDeleteSql = String.format(BATCH_DELETE_SQL, tableName);
	}

	public String getSelectIdSql() {
		return selectIdSql;
	}

	public String getExistSql() {
		return existSql;
	}

	public String getInsertSql() {
		return insertSql;
	}

	public String getDeleteSql() {
		return deleteSql;
	}

	public String getBatchDeleteSql() {
		return batchDeleteSql;
	}
}
